package com.huii.huiiplayer.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import app.BaseFragment;
import entities.SongEntity;

public class MineFragment extends BaseFragment {
	private Context mContext = getActivity();
	private List<SongEntity> list;

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		getLocalSongs();
	}

	@Override
	public View initView(LayoutInflater inflater, ViewGroup container) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseFragment getInstance() {
		// TODO Auto-generated method stub
		return null;
	}

	private List<SongEntity> getLocalSongs() {

		list = new ArrayList<SongEntity>();
		ContentResolver resolver = mContext.getContentResolver();
		Cursor cursor = resolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		while (cursor.moveToNext()) {
			SongEntity song = new SongEntity();

			int id = cursor.getInt(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
			// 歌曲id
			int trackId = cursor.getInt(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
			// 歌曲标题
			String title = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
			// 歌曲的专辑名：MediaStore.Audio.Media.ALBUM
			String album = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
			// 歌曲的歌手名： MediaStore.Audio.Media.ARTIST
			String artist = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
			// 歌曲文件的路径 ：MediaStore.Audio.Media.DATA
			String url = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
			// 歌曲的总播放时长：MediaStore.Audio.Media.DURATION
			int duration = cursor.getInt(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
			// 歌曲文件的大小 ：MediaStore.Audio.Media.SIZE
			Long size = cursor.getLong(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
			// 歌曲文件显示名字
			String disName = cursor
					.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));

			song.setSongId(trackId);
			song.setSongName(title);
			song.setAlbumName(album);
			song.setArtistName(artist);
			song.setSongLink(url);
			song.setTime(duration);

			list.add(song);
		}
		cursor.close();
		Log.i("songlist", list.toString());
		return list;
	}
}
